package com.leiting.mail.util;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class MailReadToolkit {
    private Folder folder = null;

    /**
     * 构造方法
     * @param mailType 要操作的邮箱类型
     * @param account 账号
     * @param password 密码或者是授权码
     * @param debug 是否是调试模式
     */
    public MailReadToolkit(String mailType,final String account,final String password,boolean debug){
        try {
            folder = FolderBuilder.build(mailType,account,password,debug);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 构造方法
     * @param mailType 要操作的邮箱类型
     * @param account 账号
     * @param password 密码或者是授权码
     */
    public MailReadToolkit(String mailType,final String account,final String password){
        try {
            folder = FolderBuilder.build(mailType,account,password,false);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取指定数量的邮件
     * @param mailnum
     * @return
     * @throws MessagingException
     */
    public List<Message> getMails(Integer mailnum) throws MessagingException {
        Message [] messages = null;
        int total = folder.getMessageCount();
        if(null != mailnum && mailnum > 0 && mailnum < total){
            messages = folder.getMessages(total-mailnum+1,total);
        }else {
            messages = folder.getMessages();
        }
        List<Message> messageList = null;
        if (null != messages) {
            messageList = new ArrayList<Message>();
            Collections.addAll(messageList,messages);
            Collections.reverse(messageList);
        }
        return messageList;
    }

    /**
     * 获取所有的邮件，与邮箱的中读取天数有关
     * @return
     * @throws MessagingException
     */
    public List<Message> getAllMails() throws MessagingException {
        return this.getMails(null);
    }

    /**
     * 获取邮件数量
     * @return
     */
    public Integer getCount(){
        try {
            return folder.getMessageCount();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取指定邮件，从1开始
     * @param index
     * @return
     * @throws Exception
     */
    public Message getMail(Integer index) throws Exception {
        int total = folder.getMessageCount();
        Message message =folder.getMessage(total-index+1);
        return message;
    }

    /**
     * 分页读取邮件
     * @param page 页数
     * @param pagenum 每页邮件数量
     * @return
     * @throws Exception
     */
    public List<Message> getMailListByPage( Integer page, Integer pagenum) throws Exception {
        List<Message> messageList = new ArrayList<Message>();
        int total = folder.getMessageCount();
        int begin,end;
        if(null == page || page <= 0){
            page = 1;
        }
        if(null == pagenum || pagenum <= 0){
            pagenum = 50;
        }

        begin = ((page -1) * pagenum ) +1;
        end = page * pagenum;

        begin = total - begin +1;
        end = total - end +1;

        if(begin <= 0){
            return null;
        }

        if (begin > total) {
            begin = total;
        }

        if (end <= 0) {
            end = 1;
        }

        for(int i = begin; i >= end; --i) {
            messageList.add(folder.getMessage(i));
        }

        return messageList;
    }
}
